<?php
/**
 * Created by PhpStorm.
 * userValidate: hms
 * Date: 2018/8/7
 * Time: 9:27
 */

namespace app\index\controller;

use think\Controller;
use think\Config;
use redis\Redis;
use think\Db;
use think\Request;
use think\Session;
use app\wap\model\DataUser;
use app\index\model\Member;
use app\index\model\Order;
use app\index\model\Project;
use app\wap\model\Getcash;
use app\wap\model\Recharge;
use app\wap\validate\userValidate;
use app\Tools\VerifyCodeImage;

class Dataofuser extends Controller

{
    const PERPAGE = 10;
    const MOBILE=[13795006969,18971880395,18229316988]; // 会产生拦截的电话



    /**
     * 登录
     * @param Request $request
     * @param app\api\model\User $user
     * @return \think\response\Json
     * @author cpy(1442435897@qq.com)
     */
    public function login(Request $request, DataUser $user) {
        $param = $request->param();
        $redis = Redis::instance();
        if (Session::has('code')) {
            $code = Session::get('code');
            if (strtolower($code) === strtolower(trim($param['code']))) {
                if ($param['mobile'] && $param['password']) {
                    $where['mobile'] = ['eq', trim($param['mobile'])];
                    $user_info = $user->getUserInfo($where);
                    if ($user_info) {
                        $password = sha1(md5(trim($param['password'] . $user_info['salt'])));
                        if ($password == $user_info['password']) {
                            Db::startTrans();
                            try {
                                $token = accessToken();
                                if (!empty($user_info['token']))
                                    $redis->hDel('data_user', $user_info['front_token']);
                                $user_info['login_time'] = time();
                                $user_info['front_token'] = $token;
                                $redis->hSet('data_user', $token, json_encode($user_info));
                                $redis->set('data_user_info:' . $user_info['id'], $token);
                                Session('user_id', $user_info['id']);
                                $user->save(['token' => $token], ['mobile' => $param['mobile']]);
                                Session::delete('code');
                                Db::commit();
                                $result = Config::get('code.success');
                                $result['token'] = $token;
                                $result['mobile'] = $user_info['mobile'];
                            } catch (\Exception $e) {
                                Db::rollback();
                                return $e->getMessage();
                            }
                        } else {
                            $result['msg'] = '密码错误';
                            $result['status'] = 203;
                        }
                    } else {
                        $result['msg'] = '该手机号未注册';
                        $result['status'] = 203;
                    }
                } else {
                    $result = Config::get('code.pwdError');
                }
            }else{
                $result = Config::get('code.codeFail');
            }

        }else{
            $result = Config::get('code.mistake');
        }
        return json($result);
    }

    /**
     * 退出
     * @param Request $request
     * @return \think\response\Json
     * @author cpy(1442435897@qq.com)
     */
    public function loginout(Request $request)
    {
        $user_info = $request->header();
        $redis = Redis::instance();
        $res = $redis->hDel('data_user', $user_info['token']);
        if($res){
            Session::delete('user_id');
            $result = Config::get('code.success');
        }else{
            $result = Config::get('code.error');
        }
        return json($result);
    }

    /**
     * 首页8人实时业绩
     * @param Request $request
     * @return \think\response\Json
     * @author  ky(997032813@qq.com)
     */
    public function getTotalList(Request $request, Order $order)
    {
        $dataList = [
            [
                'user_id'=>14,
                'name'=>'王艳红',
                'mobile'=>'13653866679'
            ],
            [
                'user_id'=>10,
                'name'=>'邵迎迎',
                'mobile'=>'13936808483'
            ],
            [
                'user_id'=>11,
                'name'=>'曾莉',
                'mobile'=>'15808070600'
            ],
            [
                'user_id'=>12,
                'name'=>'苏清山',
                'mobile'=>'13795006969',
                'cut_ids'=>'13,15'
            ],
            [
                'user_id'=>13,
                'name'=>'王淑会',
                'mobile'=>'15804188119'
            ],
            [
                'user_id'=>15,
                'name'=>'王欢欢',
                'mobile'=>'18841825376'
            ],
            [
                'user_id'=>16,
                'name'=>'郑红梅',
                'mobile'=>'18971880395',
                'cut_ids'=>'11'
            ],
            [
                'user_id'=>19,
                'name'=>'何莉飞',
                'mobile'=>'18229316988',
                'cut_ids'=>'14'
            ],
        ];
        //查询ids中对应的下级会员投资数据
        $param = $request->param();
        if(!empty($param['type']) && intval($param['type']>1)){ // 拦截业绩
            foreach ($dataList as $key=>$val) {
                if(!empty($val['cut_ids'])){
                    $sql="SELECT sum(money) from tf_order
                    where status >1 and type=1
                    and uid in (SELECT user_id from tf_b_gx where parent_id =".$val['user_id']." and user_id not in(SELECT user_id from tf_b_gx where parent_id in(".$val['cut_ids'].")))";
                }else{
                    $sql = "select sum(money) from tf_order where status >1 and type=1  and  uid in (select user_id from tf_b_gx where parent_id=".$val['user_id'].")";
                }
                $result = Db::query($sql);
                $dataList[$key]['total_money'] =intval($result[0]['sum(money)']);
            }
        }else{ // 未拦截
            foreach ($dataList as $key=>$val) {
                $sql = "select sum(money) from tf_order where status> 1 and type=1 and  uid in (select user_id from tf_b_gx where parent_id=".$val['user_id'].")";
                $result = Db::query($sql);
                $dataList[$key]['total_money'] = intval($result[0]['sum(money)']);
            }
        }
        if(!empty($param['to_excel']) && intval($param['to_excel'])==1){
            $tableHeader = ['ID','姓名','电话','金额'];
            foreach($result as $key=>&$value){
                if(!empty($value['cut_ids'])) unset($value['cut_ids']);
            }
            ToPHPEXECL($tableHeader,$result,'团队业绩列表');
        }

        outPutData(array('list'=>$dataList));

    }

    /**
     * 查询用户下级
     * @param Request $request
     * @return \think\response\Json
     * @author  ky(997032813@qq.com)
     */
    public function getMemberInfo(Request $request, Order $order,Member $member)
    {
        $param = $request->param();
        if(empty($param['mobile'])) outPutData('','请上传手机号',Config::get('code.error'));;
        $user_info = $member->field('id,username,mobile,role')->where(['mobile'=>trim($param['mobile'])])->find();
        if ($user_info) {
            if(intval($user_info['role']==4)){
                $user_info['name'] ='渠道总监';
            }elseif(intval($user_info['role']==3)){
                $user_info['name'] ='业务经理';
            }elseif(intval($user_info['role']==2)){
                $user_info['name'] ='销售顾问';
            }else{
                $user_info['name'] ='普通会员';
            }
            //查询我的业绩
            $sql = "select sum(money) from tf_order where status>1 and type=1 and  uid in (select user_id from tf_b_gx where parent_id=".$user_info['id'].")";
            $result = Db::query($sql);
            $user_info['total_money'] = intval($result[0]['sum(money)']);

            // 查询我的投资
            if(!empty($param['project_id'])){
                $self_sql = "select sum(money) from tf_order where status>1 and type=1 and uid=".$user_info['id']." and  project_id in(".trim($param['project_id']).")";
            }else{
                $self_sql = "select sum(money) from tf_order where status>1 and type=1 and uid=".$user_info['id'];
            }
            $result = Db::query($self_sql);
            $user_info['self_money'] = intval($result[0]['sum(money)']);
            //查询下级会员

            //销售顾问
            $xs_sql = "select count(id) from tf_member where role=2 and id in (select user_id from tf_b_gx where parent_id=".$user_info['id'].")";
            $xs = Db::query($xs_sql);
            $xs_total = $xs[0]['count(id)'];
            //业务经理
            $yw_sql = "select count(id) from tf_member where role=3 and id in (select user_id from tf_b_gx where parent_id=".$user_info['id'].")";
            $yw = Db::query($yw_sql);
            $yw_total = $yw[0]['count(id)'];

            //渠道总监
            $qd_sql = "select count(id) from tf_member where role=4 and id in (select user_id from tf_b_gx where parent_id=".$user_info['id'].")";
            $qd = Db::query($qd_sql);
            $qd_total = $qd[0]['count(id)'];
            $user_info['grade'] = [
                [
                    'total_member'=>$xs_total,
                    'name'=>'销售顾问',
                    'role'=>'2'
                ],
                [
                    'total_member'=>$yw_total,
                    'name'=>'业务经理',
                    'role'=>'3'
                ],
                [
                    'total_member'=>$qd_total,
                    'name'=>'渠道总监',
                    'role'=>'4'
                ]
            ];
        } else {
            $result = [
                'data'=>'',
                'info'=>'用户不存在',
            ];
            outPutData($result);
        }

        outPutData(array('list'=>$user_info));

    }

    /**
     * 高级查询
     * @param Request $request
     * @return \think\response\Json
     * @author  ky(997032813@qq.com)
     */
    public function getSenior(Request $request, Order $order,Member $member)
    {
        $param = $request->param();
        if (empty($param['mobile'])) {
            outPutData('','参数错误，缺少手机号');

        }
        if (empty($param['start_time']) && empty($param['end_time']) && empty($param['project_id'])) {
            outPutData('','参数错误,请至少选择项目与时间中的一个');

        }
        $user_info = $member->field('id,username')->where(['mobile'=>$param['mobile']])->find();

        if (empty($user_info)) {
            outPutData('','用户不存在',Config::get('code.error'));

        } else { // 步入查询
            if(trim($param['mobile'])=='13795006969') $cut_ids='13,15'; //苏青山
            if(trim($param['mobile'])=='18971880395') $cut_ids='11'; //郑红梅
            if(trim($param['mobile'])=='18229316988') $cut_ids='14'; //何莉飞
            if(!empty($cut_ids) && !empty($param['is_cut']) && $param['is_cut']>1){ // 拦截
                if ($param['start_time'] && $param['end_time']) {
                    if ($param['project_id']) {
                        //查询我下面的业绩
                        if(count(explode(',',$param['project_id']))>1){
                            $sql = "select sum(money) from tf_order where project_id in(".$param['project_id'].")and status>1 and type=1  and  uid in (select user_id from tf_b_gx where parent_id=".$user_info['id'].") and user_id not in(SELECT user_id from tf_b_gx where parent_id in(".$cut_ids."))";
                        }else{
                            $sql = "select sum(money) from tf_order where project_id in(".$param['project_id'].")and status>1 and type=1 and create_time>".$param['start_time']." and create_time<".$param['end_time']." and  uid in (select user_id from tf_b_gx where parent_id=".$user_info['id'].") and user_id not in(SELECT user_id from tf_b_gx where parent_id in(".$cut_ids."))";
                        }
                        $result = Db::query($sql);
                        if (!$result[0]['sum(money)']) {
                            $result[0]['sum(money)'] = 0;
                        }
                        $user_info['total_money'] = $result[0]['sum(money)'];
                    } else {
                        $sql = "select sum(money) from tf_order where status>1 and type=1 and create_time>".$param['start_time']." and create_time<".$param['end_time']." and uid in (select user_id from tf_b_gx where parent_id=".$user_info['id'].")and user_id not in(SELECT user_id from tf_b_gx where parent_id in(".$cut_ids."))";
                        $result = Db::query($sql);
                        $user_info['total_money'] = $result[0]['sum(money)'];
                    }

                } elseif($param['start_time']){
                    if ($param['project_id']) {
                        if(count(explode(',',$param['project_id']))>1){
                            $sql = "select sum(money) from tf_order where status>1 and type=1 and  project_id in(".$param['project_id'].")  and uid in (select user_id from tf_b_gx where parent_id=".$user_info['id'].")and user_id not in(SELECT user_id from tf_b_gx where parent_id in(".$cut_ids."))";
                        }else{
                            $sql = "select sum(money) from tf_order where status>1 and type=1 and  project_id in(".$param['project_id'].")  and create_time>".$param['start_time']." and uid in (select user_id from tf_b_gx where parent_id=".$user_info['id'].")and user_id not in(SELECT user_id from tf_b_gx where parent_id in(".$cut_ids."))";
                        }
                        $result = Db::query($sql);
                        $user_info['total_money'] = $result[0]['sum(money)'];
                    } else {
                        $sql = "select sum(money) from tf_order where status>1 and type=1 and create_time>".$param['start_time']." and  uid in (select user_id from tf_b_gx where parent_id=".$user_info['id'].")and user_id not in(SELECT user_id from tf_b_gx where parent_id in(".$cut_ids."))";
                        $result = Db::query($sql);
                        $user_info['total_money'] = $result[0]['sum(money)'];
                    }
                }elseif($param['end_time']){
                    if ($param['project_id']) {
                        if(count(explode(',',$param['project_id']))>1){
                            $sql = "select sum(money) from tf_order where status>1 and type=1 and  project_id in(".$param['project_id'].") and uid in (select user_id from tf_b_gx where parent_id=".$user_info['id'].")and user_id not in(SELECT user_id from tf_b_gx where parent_id in(".$cut_ids."))";
                        }else{
                            $sql = "select sum(money) from tf_order where status>1 and type=1 and  project_id in(".$param['project_id'].") and create_time<".$param['end_time']." and uid in (select user_id from tf_b_gx where parent_id=".$user_info['id'].")and user_id not in(SELECT user_id from tf_b_gx where parent_id in(".$cut_ids."))";
                        }
                        $result = Db::query($sql);
                        $user_info['total_money'] = $result[0]['sum(money)'];
                    } else {
                        $sql = "select sum(money) from tf_order where status>1 and type=1 and create_time<".$param['end_time']." and uid in (select user_id from tf_b_gx where parent_id=".$user_info['id'].")and user_id not in(SELECT user_id from tf_b_gx where parent_id in(".$cut_ids."))";
                        $result = Db::query($sql);
                        $user_info['total_money'] = $result[0]['sum(money)'];
                    }
                }else { // 没有开始结束日期
                    if ($param['project_id']) {
                        $sql = "select sum(money) from tf_order where status>1 and type=1 and  project_id in(".$param['project_id'].") and uid in (select user_id from tf_b_gx where parent_id=".$user_info['id'].")and user_id not in(SELECT user_id from tf_b_gx where parent_id in(".$cut_ids."))";
                        $result = Db::query($sql);
                        $user_info['total_money'] = $result[0]['sum(money)'];
                    } else {
                        $sql = "select sum(money) from tf_order where status>1 and type=1 and uid in (select user_id from tf_b_gx where parent_id=".$user_info['id'].")and user_id not in(SELECT user_id from tf_b_gx where parent_id in(".$cut_ids."))";
                        $result = Db::query($sql);
                        $user_info['total_money'] = $result[0]['sum(money)'];
                    }

                }
            }else{
                if ($param['start_time'] && $param['end_time']) {
                    if ($param['project_id']) {
                        //查询我下面的业绩
                        if(count(explode(',',$param['project_id']))>1){
                            $sql = "select sum(money) from tf_order where project_id in(".$param['project_id'].")and status>1 and type=1 and  uid in (select user_id from tf_b_gx where parent_id=".$user_info['id'].")";
                        }else{
                            $sql = "select sum(money) from tf_order where project_id in(".$param['project_id'].")and status>1 and type=1 and create_time>".$param['start_time']." and create_time<".$param['end_time']." and  uid in (select user_id from tf_b_gx where parent_id=".$user_info['id'].")";
                        }
                        $result = Db::query($sql);
                        if (!$result[0]['sum(money)']) {
                            $result[0]['sum(money)'] = 0;
                        }
                        $user_info['total_money'] = $result[0]['sum(money)'];
                    } else {
                        $sql = "select sum(money) from tf_order where status>1 and type=1 and create_time>".$param['start_time']." and create_time<".$param['end_time']." and uid in (select user_id from tf_b_gx where parent_id=".$user_info['id'].")";
                        $result = Db::query($sql);
                        $user_info['total_money'] = $result[0]['sum(money)'];
                    }

                } elseif($param['start_time']){
                    if ($param['project_id']) {
                        if(count(explode(',',$param['project_id']))>1){
                            $sql = "select sum(money) from tf_order where status>1 and type=1 and  project_id in(".$param['project_id'].") and uid in (select user_id from tf_b_gx where parent_id=".$user_info['id'].")";
                        }else{
                            $sql = "select sum(money) from tf_order where status>1 and type=1 and  project_id in(".$param['project_id'].")  and create_time>".$param['start_time']." and uid in (select user_id from tf_b_gx where parent_id=".$user_info['id'].")";
                        }
                        $result = Db::query($sql);
                        $user_info['total_money'] = $result[0]['sum(money)'];
                    } else {
                        $sql = "select sum(money) from tf_order where status>1 and type=1 and create_time>".$param['start_time']." and  uid in (select user_id from tf_b_gx where parent_id=".$user_info['id'].")";
                        $result = Db::query($sql);
                        $user_info['total_money'] = $result[0]['sum(money)'];
                    }
                }elseif($param['end_time']){
                    if ($param['project_id']) {
                        if(count(explode(',',$param['project_id']))>1){
                            $sql = "select sum(money) from tf_order where status>1 and type=1 and  project_id in(".$param['project_id'].") and uid in (select user_id from tf_b_gx where parent_id=".$user_info['id'].")";
                        }else{
                            $sql = "select sum(money) from tf_order where status>1 and type=1 and  project_id in(".$param['project_id'].") and create_time<".$param['end_time']." and uid in (select user_id from tf_b_gx where parent_id=".$user_info['id'].")";
                        }
                        $result = Db::query($sql);
                        $user_info['total_money'] = $result[0]['sum(money)'];
                    } else {
                        $sql = "select sum(money) from tf_order where status>1 and type=1 and create_time<".$param['end_time']." and uid in (select user_id from tf_b_gx where parent_id=".$user_info['id'].")";
                        $result = Db::query($sql);
                        $user_info['total_money'] = $result[0]['sum(money)'];
                    }
                }else { // 没有开始结束日期
                    if ($param['project_id']) {
                        $sql = "select sum(money) from tf_order where status>1 and type=1 and  project_id in(".$param['project_id'].") and uid in (select user_id from tf_b_gx where parent_id=".$user_info['id'].")";
                        $result = Db::query($sql);
                        $user_info['total_money'] = $result[0]['sum(money)'];
                    } else {
                        $sql = "select sum(money) from tf_order where status>1 and type=1 and uid in (select user_id from tf_b_gx where parent_id=".$user_info['id'].")";
                        $result = Db::query($sql);
                        $user_info['total_money'] = $result[0]['sum(money)'];
                    }

                }
            }


        }
        $start_time = empty($param['start_time'])?0:$param['start_time'];
        $end_time = empty($param['end_time'])?0:time();
        if(!empty($param['project_id'])){
            if(count(explode(',',$param['project_id']))>1){
                $self_sql = "select sum(money) from tf_order where status>1 and type=1 and uid=".$user_info['id']." and  project_id in(".$param['project_id'].")";
            }else{
                $self_sql = "select sum(money) from tf_order where status>1 and type=1 and uid=".$user_info['id']." and create_time > $start_time and create_time < $end_time and project_id in (".$param['project_id'].")";
            }

        }else{
            $self_sql = "select sum(money) from tf_order where status>1 and type=1 and create_time > $start_time and create_time < $end_time and uid=".$user_info['id'];
        }
        $self_result = Db::query($self_sql);
        $user_info['total_money']=intval($user_info['total_money']);
        $user_info['self_money'] = intval($self_result[0]['sum(money)']);
        outPutData($user_info);

    }


    /**
     * 对应用户下级级别查询
     * @param Request $request
     * @return \think\response\Json
     * @author  ky(997032813@qq.com)
     */

    public function getSubordinate(Request $request, Order $order,Member $member)
    {
        $param = $request->param();
        if (empty($param['mobile'])) {
            outPutData('','参数错误，缺少手机号');

        }
        $user_info = $member->field('id,username')->where(['mobile'=>$param['mobile']])->find();

        if (empty($user_info)) {
            outPutData('','用户不存在');

        } else {
            if (!$param['role']) {
                outPutData('','缺少等级参数');
            } else {
                //$sql = "select a.username,a.mobile,sum(b.money) as money from tf_member as a left join tf_order as b on a.id=b.uid where b.status >1 and type=1  and a.role=".$param['role']." and a.id in (select user_id from tf_b_gx where parent_id=".$user_info['id'].") group by a.username";
                //查询对应下级
                $sql = "select id,username,mobile from tf_member where role=".$param['role']." and id in (select user_id from tf_b_gx where parent_id=".$user_info['id'].")";
                $result = Db::query($sql);
                foreach ($result as $key=>$val) {
                    if(!empty($param['project_id'])){
                        $order = "select sum(money) as money from tf_order where status >1 and type=1 and project_id in(".trim($param['project_id']).")  and uid in (select user_id from tf_b_gx where parent_id=".$val['id'].")";
                    }else{
                        $order = "select sum(money) as money from tf_order where status >1 and type=1  and uid in (select user_id from tf_b_gx where parent_id=".$val['id'].")";
                    }

                    $order_result = Db::query($order);
                    if (empty($order_result)) {
                        $result[$key]['money'] = '0';
                    } else {
                        $result[$key]['money'] = $order_result[0]['money'];
                    }
                    // 对应的自己投资金额
                    if(!empty($param['project_id'])){
                        $self_sql = "select sum(money) from tf_order where status>1 and type=1 and uid=".$user_info['id']." and  project_id in(".trim($param['project_id']).")";
                    }else{
                        $self_sql = "select sum(money) from tf_order where status>1 and type=1 and uid=".$user_info['id'];
                    }
                    $order_result = Db::query($self_sql);
                    if (empty($order_result)) {
                        $result[$key]['self_money'] = '0';
                    } else {
                        $result[$key]['self_money'] = $order_result[0]['sum(money)'];
                    }
                }
                if ($result) {
                    $field = array_column($result, 'money');
                    array_multisort($field,SORT_DESC,$result);
                }
                if($param['to_excel']){
                    $tableHeader = ['ID','姓名','电话','业绩','投资总额'];
                    ToPHPEXECL($tableHeader,$result,'会员列表');
                }

            }

        }



        outPutData($result);

    }


    /**
     * 获取项目
     * @param Request $request
     * @return \think\response\Json
     * @author  ky(997032813@qq.com)
     */

    public function getProject(Request $request, Project $project)
    {
        $data = $project->field('id,project_name')->select();


        outPutData($data);

    }

    /**
     * 获取会员直推列表
     * @param Request $request
     * @return \think\response\Json
     * @author  ky(997032813@qq.com)
     */

    public function getZtMember(Request $request, Member $member)
    {
        $param = $request->param();
        if (empty($param['member_id'])) {
            outPutData('','参数错误');

        }
        $user_info = $member->field('id,username')->where(['id'=>$param['member_id']])->find();

        if (empty($user_info)) {
            outPutData('','用户不存在');

        }
        $sql = "select user_id from tf_b_gx where gx=1 and  parent_id=".$param['member_id'];
        $result = Db::query($sql);
        $user_ids = array_column($result,'user_id');
        $user_ids = implode(',',$user_ids);

        $return = [];
        if (!empty($user_ids)) {
            $total_money = 0;
            $sql = "select a.id,a.username,a.mobile 
                    from tf_member as a 
                    where 
                      a.id in (".$user_ids.") 
                    group by a.id";
            $data = Db::query($sql);
            foreach ($data as $key=>$val) {
                $data[$key]['money'] = 0;
                if (empty($val['username'])) {
                    $data[$key]['username'] = '未认证会员';
                } else {
                    if(!empty($param['project_id'])){ // 选择项目
                        $order_sql = "select sum(money) money from tf_order where status >1 and type=1 and project_id in(".trim($param['project_id']).")   and uid = ".$val['id'];
                    }else{
                        $order_sql = "select sum(money) money from tf_order where status >1 and type=1   and uid = ".$val['id'];
                    }

                    $order_result = Db::query($order_sql);
                    if (!empty($order_result[0]['money'])) {
                        $data[$key]['money'] = $order_result[0]['money'];
                    }
                }
            }
            $return['username'] = $user_info['username'];
            $return['count_member'] = count($result);
            if ($data) {
                $field = array_column($data, 'money');
                array_multisort($field,SORT_DESC,$data);
            }
            foreach($data as $key=>$val) {
                $total_money +=$val['money'];
                $daochu[$key]['id'] = $val['id'];
                $daochu[$key]['username'] = $val['username'];
                $daochu[$key]['mobile'] = $val['mobile'];
                $daochu[$key]['money'] = $val['money'];
            }

            $return['total_money'] = $total_money;
            $return['list'] = $data;

        }

        if($param['to_excel']){
            $tableHeader = ['ID','姓名','电话','金额'];
            ToPHPEXECL($tableHeader,$daochu,'会员列表');
        } else {
            outPutData($return);
        }


    }


    /**
     * 单项目团队业绩 包含野生团队
     * @param Request $request
     * @return \think\response\Json
     * @author  hey_heyang(635870453@qq.com)
     */
    public function getSingleTotalList(Request $request, Order $order)
    {
        $dataList = [
            [
                'user_id'=>14,
                'name'=>'王艳红',
                'mobile'=>'13653866679'
            ],
            [
                'user_id'=>10,
                'name'=>'邵迎迎',
                'mobile'=>'13936808483'
            ],
            [
                'user_id'=>11,
                'name'=>'曾莉',
                'mobile'=>'15808070600'
            ],
            [
                'user_id'=>12,
                'name'=>'苏清山',
                'mobile'=>'13795006969',
                'cut_ids'=>'13,15'
            ],
            [
                'user_id'=>13,
                'name'=>'王淑会',
                'mobile'=>'15804188119'
            ],
            [
                'user_id'=>15,
                'name'=>'王欢欢',
                'mobile'=>'18841825376'
            ],
            [
                'user_id'=>16,
                'name'=>'郑红梅',
                'mobile'=>'18971880395',
                'cut_ids'=>'11'
            ],
            [
                'user_id'=>19,
                'name'=>'何莉飞',
                'mobile'=>'18229316988',
                'cut_ids'=>'14'
            ],
        ];
        //查询ids中对应的下级会员投资数据
        $param = $request->param();
        if(!empty($param['type']) && intval($param['type']>1)){ // 拦截业绩
            foreach ($dataList as $key=>$val) {
                if(!empty($param['project_id'])){ // 指定项目
                    if(!empty($val['cut_ids'])){
                        $sql="SELECT sum(money) from tf_order
                    where status >1 and type=1 and project_id in(".trim($param['project_id']).")
                    and uid in (SELECT user_id from tf_b_gx where parent_id =".$val['user_id']." and user_id not in(SELECT user_id from tf_b_gx where parent_id in(".$val['cut_ids'].")))";
                    }else{
                        $sql = "select sum(money) from tf_order where status >1 and type=1 and project_id in(".trim($param['project_id']).")  and  uid in (select user_id from tf_b_gx where parent_id=".$val['user_id'].")";
                    }
                }else{ // 全部项目
                    if(!empty($val['cut_ids'])){
                        $sql="SELECT sum(money) from tf_order
                    where status >1 and type=1
                    and uid in (SELECT user_id from tf_b_gx where parent_id =".$val['user_id']." and user_id not in(SELECT user_id from tf_b_gx where parent_id in(".$val['cut_ids'].")))";
                    }else{
                        $sql = "select sum(money) from tf_order where status >1 and type=1  and  uid in (select user_id from tf_b_gx where parent_id=".$val['user_id'].")";
                    }
                }

                $result = Db::query($sql);
                $dataList[$key]['total_money'] = intval($result[0]['sum(money)']);
            }
        }else{ // 未拦截
            foreach ($dataList as $key=>$val) {
                if(!empty($param['project_id'])){
                    $sql = "select sum(money) from tf_order where status> 1 and type=1 and project_id in(".trim($param['project_id']).") and  uid in (select user_id from tf_b_gx where parent_id=".$val['user_id'].")";
                }else{
                    $sql = "select sum(money) from tf_order where status> 1 and type=1 and  uid in (select user_id from tf_b_gx where parent_id=".$val['user_id'].")";
                }
                $result = Db::query($sql);
                $dataList[$key]['total_money'] = intval($result[0]['sum(money)']);
            }
        }
        //野生团队
        if(!empty($param['project_id'])){
            $ys_sql ="SELECT sum(money)from tf_order
                where status>1 and type=1
                 and project_id in(".trim($param['project_id']).")
                and uid not in
                (SELECT user_id from tf_b_gx where parent_id in (10,11,12,13,14,15,16,19))
                and uid not in(10,11,12,13,14,15,16,19)";
        }else{
            $ys_sql ="SELECT sum(money)from tf_order
                where status>1 and type=1
                and uid not in
                (SELECT user_id from tf_b_gx where parent_id in (10,11,12,13,14,15,16,19))
                and uid not in(10,11,12,13,14,15,16,19)";
        }
        $result = Db::query($ys_sql);
        $dataList[$key+1]['user_id'] =-1;
        $dataList[$key+1]['name'] = '野生';
        $dataList[$key+1]['mobile'] = 'xxxxxxxxxxx';
        $dataList[$key+1]['total_money'] = intval($result[0]['sum(money)']);
        if(!empty($param['to_excel']) && intval($param['to_excel'])==1){
            $tableHeader = ['ID','姓名','电话','金额'];
            foreach($result as $key=>&$value){
                if(!empty($value['cut_ids'])) unset($value['cut_ids']);
            }
            ToPHPEXECL($tableHeader,$result,'项目团队业绩列表');
        }

        outPutData(array('list'=>$dataList));

    }

    /**
     * 返回8大创始人与野生团队
     * @param Request $request
     * @return \think\response\Json
     * @author  hey_heyang(635870453@qq.com)
     */
    public function getOriginList(Request $request, Order $order)
    {
        $dataList = [
            [
                'user_id' => 14,
                'name' => '王艳红',
                'mobile' => '13653866679'
            ],
            [
                'user_id' => 10,
                'name' => '邵迎迎',
                'mobile' => '13936808483'
            ],
            [
                'user_id' => 11,
                'name' => '曾莉',
                'mobile' => '15808070600'
            ],
            [
                'user_id' => 12,
                'name' => '苏清山',
                'mobile' => '13795006969',
                'cut_ids' => '13,15'
            ],
            [
                'user_id' => 13,
                'name' => '王淑会',
                'mobile' => '15804188119'
            ],
            [
                'user_id' => 15,
                'name' => '王欢欢',
                'mobile' => '18841825376'
            ],
            [
                'user_id' => 16,
                'name' => '郑红梅',
                'mobile' => '18971880395',
                'cut_ids' => '11'
            ],
            [
                'user_id' => 19,
                'name' => '何莉飞',
                'mobile' => '18229316988',
                'cut_ids' => '14'
            ],
            [
                'user_id' => -1,
                'name' => '野生',
                'mobile' => 'xxxxxxxxxxx'
            ],
        ];
        outPutData($dataList);
    }

    /**
     * 导出新版拦截团队数据
     * @param Request $request
     * @return \think\response\Json
     * @author  hey_heyang(635870453@qq.com)
     */
    public function excelTeamData(Request $request, Order $order)
    {
        $param = $request->param();
        if (empty($param['user_id'])) {
            outPutData('','参数错误',Config::get('code.error'));

        }
        $mModel = new Member();
        $oModel = new Order();
        $where1 = [];
        if(intval($param['user_id'])!=-1){
            if(intval($param['user_id'])==12){ //苏青山
                $sql = "SELECT DISTINCT uid from tf_order where status>1 and type=1 and uid in (SELECT user_id from tf_b_gx where parent_id in(".$param['user_id'].") and user_id not in(SELECT user_id from tf_b_gx where parent_id in(13,15)))";
            }elseif(intval($param['user_id'])==16){ // 郑红梅
                $sql = "SELECT DISTINCT uid from tf_order where status>1 and type=1 and uid in (SELECT user_id from tf_b_gx where parent_id in(".$param['user_id'].") and user_id not in(SELECT user_id from tf_b_gx where parent_id in(11)))";
            }elseif(intval($param['user_id'])==19){  // 何莉飞
                $sql = "SELECT DISTINCT uid from tf_order where status>1 and type=1 and uid in (SELECT user_id from tf_b_gx where parent_id in(".$param['user_id'].") and user_id not in(SELECT user_id from tf_b_gx where parent_id in(14)))";
            }else{
                $sql = "SELECT DISTINCT uid from tf_order where status>1 and type=1 and uid in (SELECT user_id from tf_b_gx where parent_id in(".$param['user_id']."))";
            }
            $result = Db::query($sql);
            $user_ids = array_column($result,'uid');
            $user_ids = implode(',',$user_ids);
        }else{
            $sql = "SELECT  DISTINCT uid from tf_order where status>1 and type=1 and uid not in (SELECT user_id from tf_b_gx where parent_id in (10,11,12,13,14,15,16,19)) and uid not in(10,11,12,13,14,15,16,19)";
            $result = Db::query($sql);
            $user_ids = array_column($result,'uid');
            $user_ids = implode(',',$user_ids);
        }
        $where1['id'] = ['in',$user_ids];

        $tableHeader = ['用户id','用户姓名','手机号','那些年的青春','三国之战神无双','暗黑者','缉妖法海传','伏妖天师','争分夺秒','那座城，那个人','郊区的鸟','追·踪','霍家拳之铁臂娇娃','我们的科幻世界','被光抓走的人','封神无双志'];
        $project_ids = ['6','9','11,12,15','14','16,17','19,25,27','28,31,45,52','29','30,35','32,33,34,41','37,44','46,50','51,53']; //项目合并id
        $excel_data =array();
        $member_data = $mModel->where($where1)->field('id,username,mobile')->limit(50)->select()->toArray();
        foreach($member_data as $key=>$value){
            $excel_data[$key]['id'] = $value['id'];
            $excel_data[$key]['username'] = $value['username'];
            $excel_data[$key]['mobile'] = $value['mobile'];
            foreach($project_ids as $k=>$v){
                $money_total = $oModel->where(['status'=>['gt',1],'type'=>1,'project_id'=>['in',$v],'uid'=>$value['id']])->sum('money');
                $alias = 'project'.$k;
                $excel_data[$key][$alias] = empty($money_total)?0:$money_total;
            }

        }
        ToPHPEXECL($tableHeader,$excel_data,'拦截团队数据');
        outPutData('操作成功');
    }

    public function getPicCode()
    {
        $code = createRandomNum(4);
        Session('code',$code);
        $imageCode = new VerifyCodeImage(80,20,$code);
        $file = $imageCode->show();
        $result = Config::get('code.success');
        $result['code'] = $file;
        return json($result);
    }
}